package com.tenghu.sbm.cus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenghu.sbm.cus.entity.CusBaseInfo;
import com.tenghu.sbm.cus.mapper.CusBaseInfoMapper;
import com.tenghu.sbm.cus.service.CusBaseInfoService;
import com.tenghu.sbm.rest.RestResponse;
import com.tenghu.sbm.rest.RestResponseFactory;
import com.tenghu.sbm.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户基础信息服务实现类
 * @author Arvin
 * @date 2017/12/9 12:37
 */
@Service
public class CusBaseInfoServiceImpl implements CusBaseInfoService{

    @Autowired
    private CusBaseInfoMapper cusBaseInfoMapper;

    @Override
    public RestResponse getAllCusBaseInfo(CusBaseInfo cusBaseInfo,int currentPage,int pageSize) {
        //设置分页
        PageHelper.startPage(currentPage,pageSize);
        List<CusBaseInfo> cusBaseInfoList=cusBaseInfoMapper.getAllCusBaseInfo(cusBaseInfo);
        return RestResponseFactory.getSucResponse(new PageBean<>(new PageInfo<>(cusBaseInfoList)));
    }

    @Override
    public RestResponse saveCusBaseInfo(CusBaseInfo cusBaseInfo) {
        //根据客户名称获取客户信息
        CusBaseInfo oldCusBaseInfo=cusBaseInfoMapper.getCusBaseInfoByName(cusBaseInfo.getCusName());
        if(null!=oldCusBaseInfo){
            return RestResponseFactory.getErrorResponse("客户【"+cusBaseInfo.getCusName()+"】已存在！");
        }
        //保存客户信息
        int result=cusBaseInfoMapper.saveCusBaseInfo(cusBaseInfo);
        if(result>0){
            return RestResponseFactory.getSucResponse(cusBaseInfo);
        }
        return RestResponseFactory.getErrorResponse("客户【"+cusBaseInfo.getCusName()+"】保存失败！");
    }

    @Override
    public RestResponse getCusBaseInfoById(String cusId) {
        return RestResponseFactory.getSucResponse(cusBaseInfoMapper.getCusBaseInfoById(cusId));
    }

    @Override
    public RestResponse updateCusBaseInfo(CusBaseInfo cusBaseInfo) {
        //检查客户是否存在
        CusBaseInfo oldCusBaseInfo=cusBaseInfoMapper.getCusBaseInfoById(cusBaseInfo.getCusId());
        if(null==oldCusBaseInfo){
            return RestResponseFactory.getErrorResponse("客户【"+cusBaseInfo.getCusName()+"】不存在！");
        }
        //更新客户信息
        int result=cusBaseInfoMapper.updateCusBaseInfo(cusBaseInfo);
        if(result>0){
            return RestResponseFactory.getSucResponse("更新成功！");
        }
        return RestResponseFactory.getErrorResponse("更新失败！");
    }

    @Override
    public RestResponse deleteCusBaseInfo(String cusId) {
        int result=cusBaseInfoMapper.deleteCusBaseInfo(cusId);
        return result>0?RestResponseFactory.getSucResponse("删除成功！"):RestResponseFactory.getErrorResponse("删除失败！");
    }

    @Override
    public RestResponse deleteBatchCusBaseInfo(String[] cusIds) {
        int result=cusBaseInfoMapper.deleteBatchCusBaseInfo(cusIds);
        return result>0?RestResponseFactory.getSucResponse("删除成功！"):RestResponseFactory.getErrorResponse("删除失败！");
    }
}
